<?php

namespace common\entities\common;

use common\entities\ConfigEntity;
use Yii;

/**
 * This is the model class for table "myscrm_organizationtoauthorize".
 *
 * @property string $Id
 * @property string $OrganizationId
 * @property string $ApplicationId
 * @property string $AuthorizeTypeId
 * @property integer $TotalNum
 * @property string $VersionNumber
 * @property string $ContractId
 */
class MyscrmOrganizationToAuthorizeEntity extends ConfigEntity
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'myscrm_organizationtoauthorize';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Id', 'TotalNum', 'VersionNumber'], 'required'],
            [['TotalNum'], 'integer'],
            [['Id', 'OrganizationId', 'ApplicationId', 'AuthorizeTypeId', 'ContractId'], 'string', 'max' => 36],
            [['VersionNumber'], 'string', 'max' => 8],
            [['OrganizationId', 'ApplicationId', 'AuthorizeTypeId', 'ContractId'], 'unique', 'targetAttribute' => ['OrganizationId', 'ApplicationId', 'AuthorizeTypeId', 'ContractId'], 'message' => 'The combination of Organization ID, Application ID, Authorize Type ID and Contract ID has already been taken.']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'Id' => 'ID',
            'OrganizationId' => 'Organization ID',
            'ApplicationId' => 'Application ID',
            'AuthorizeTypeId' => 'Authorize Type ID',
            'TotalNum' => 'Total Num',
            'VersionNumber' => 'Version Number',
            'ContractId' => 'Contract ID',
        ];
    }
    
    /**
    * 根据租户id,获取租户授权信息列表
    * @param string $organizationId 租户id
    * @param string $contractId 合同id
    * @return array
    */
	public static function getAuthorizeListByOrganizationId($organizationId,$contractId) {
		return self::find()->where(['OrganizationId'=>$organizationId,'ContractId'=>$contractId])->all();
	}

    /**
     * 得到授权数
     * @param $organizationId
     * @param $contractId
     * @param $applicationId
     * @return null|static
     */
    public static function getAuthorize($organizationId, $contractId, $applicationId){
        return self::findOne([
            'OrganizationId'=>$organizationId,'ContractId'=>$contractId, 'ApplicationId' => $applicationId
        ]);
    }
}
